package cn.wx.pet.controller;


import cn.wx.basic.annotation.PreAuthorize;
import cn.wx.basic.exception.BusinessException;
import cn.wx.basic.util.GetConTest;
import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;
import cn.wx.org.domain.Employee;
import cn.wx.org.mapper.EmployeeMapper;
import cn.wx.pet.domain.SearchMasterMsg;
import cn.wx.pet.dto.AcceptDto;
import cn.wx.pet.query.SearchMasterMsgQuery;
import cn.wx.pet.service.ISearchMasterMsgService;
import cn.wx.user.domain.Logininfo;
import cn.wx.user.domain.User;
import cn.wx.user.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
@Api(value = "宠物类型的API",description="宠物类型相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService iSearchMasterMsgService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private UserMapper userMapper;


    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<SearchMasterMsg> findAll(){
        return iSearchMasterMsgService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "单独查询宠物",sn = "searchMasterMsg:get") //value后面用来做权限校验
    public SearchMasterMsg findById(@PathVariable("id") Long id ){
        return iSearchMasterMsgService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "删除宠物类型",sn = "searchMasterMsg:delete") //value后面用来做权限校验
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iSearchMasterMsgService.remove(id);
           return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //添加和更新     //使用put方式请求
    @PutMapping
    @ApiOperation(value = "增加修改" )
    public JsonResult save(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if(searchMasterMsg.getId()==null){
                iSearchMasterMsgService.save(searchMasterMsg);
            }else{
                iSearchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){

        try {
            Logininfo logininfo = GetConTest.getLogininfo(request);
            if(logininfo.getType()==0){ //表示员工登录
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                if(employee.getShop_id()!=null){   //表示店铺员工
                    query.setShopId(employee.getShop_id());
                }

            }else if(logininfo.getType()==1){  //表示用户登录
                User user = userMapper.findByLogininfoId(logininfo.getId());
                query.setUserId(user.getId());
            }
            return  iSearchMasterMsgService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }


    //批量删除
    @PatchMapping
    public JsonResult  patchDel(@RequestBody List<Long> ids){
        try {
            iSearchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }
/*
*   寻主提交
* */
    @PostMapping("/publish")
    private JsonResult publish(@RequestBody SearchMasterMsg ssm, HttpServletRequest request){
        try {
            Logininfo logininfo = GetConTest.getLogininfo(request);
            iSearchMasterMsgService.publish(ssm,logininfo.getId());
            return    JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常");
        }

    }

    //拒单
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "通过id修改 shop_id 为null" )
    public JsonResult reject(@PathVariable("id") Long id){
        try {
            iSearchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /*
    *   接单
    * */
    @PostMapping("accept")
    public JsonResult accept(@RequestBody AcceptDto dto){
        try {
            iSearchMasterMsgService.accept(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }

}
